package com.baymax.test.scene.drawable

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.LinearGradient
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.Shader
import android.graphics.drawable.Drawable


class GradientShadow : Drawable() {
    private val colors = intArrayOf(
        Color.TRANSPARENT,
        Color.argb(0, 0, 0, 0),
        Color.argb(16, 0, 0, 0),
        Color.argb(32, 0, 0, 0),
        Color.argb(64, 0, 0, 0),
        Color.argb(80, 0, 0, 0),
        Color.parseColor("#1D1E27")
    )
    private val positions = floatArrayOf(0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.53f, 1f)

    private val p = Paint(Paint.ANTI_ALIAS_FLAG)
    private var gradient: LinearGradient =
        LinearGradient(0f, 0f, 0f, 0f, 0, 0, Shader.TileMode.CLAMP)

    override fun onBoundsChange(bounds: Rect) {
        super.onBoundsChange(bounds)
        // x0 = r.left;            y0 = r.top;
        // x1 = x0;                y1 = level * r.bottom;
        //top -> bottom
        gradient = LinearGradient(
            bounds.left.toFloat(),
            bounds.top.toFloat(),
            bounds.left.toFloat(),
            bounds.height().toFloat(),
            colors,
            positions,
            Shader.TileMode.CLAMP
        ).apply {
            this.setLocalMatrix(Matrix().apply {
                setScale(1f, 1.4f)
            })
            p.setShader(this)
        }
    }

    override fun draw(canvas: Canvas) {
        // 设置渐变色显示的比例
        // 绘制矩形作为背景
        canvas.drawRect(0f, 0f, bounds.width().toFloat(), bounds.height().toFloat(), p)
    }

    override fun setAlpha(alpha: Int) {
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
    }

    override fun getOpacity(): Int {
        return PixelFormat.UNKNOWN
    }
}
